//
//  MyDocumentsFolder.m
//  Documents
//
//  Created by Alex Di Mango 
//  Copyright (c) 2012 Whardoo.com -  All rights reserved.
//

#import "MyDocumentsFolder.h"
#import "MyDocument.h"
#import "DocumentsCache.h"
#import "AppDelegate.h"


@implementation MyDocumentsFolder

@dynamic folderId;
@dynamic name;
@dynamic documents;
@dynamic defaultFolder;
@dynamic createDate;
@dynamic layer;
@dynamic color;


+(NSMutableArray *)getFolders
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyDocumentsFolder" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"folderId" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    NSError *errors = nil;
    NSMutableArray *result = [NSMutableArray arrayWithArray:[managedObjectContext executeFetchRequest:request error:&errors]];
    [request release];
    [sortDescriptor release];
    return result;
}
+(BOOL)hasBeenModified:(NSNumber *)folderId
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyDocumentsFolder" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"folderId == %@ AND modified ==YES",folderId];
    [request setPredicate:predicate];
    NSError *errors = nil;
    NSArray *resultArray = [managedObjectContext executeFetchRequest:request error:&errors];
    [request release];
    if([resultArray count] == 0)
        return NO;
    return YES;
}


+(NSMutableArray *)getFolderList
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setReturnsObjectsAsFaults:NO];
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyDocumentsFolder" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT folderId == %@",[NSNumber numberWithInt:1]];
    [request setPredicate:predicate];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"folderId" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    NSError *errors = nil;
    NSMutableArray *result = [NSMutableArray arrayWithArray:[managedObjectContext executeFetchRequest:request error:&errors]];
    [request release];
    [sortDescriptor release];
    return result;
}

+(NSNumber *)getMaxFolderId{    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    //Entity
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyDocumentsFolder" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];
    
    request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"folderId"]];    
    NSError *errors = nil;
    NSArray *result = [managedObjectContext executeFetchRequest:request error:&errors];
    [request release];
    return [result valueForKeyPath:@"@max.folderId"];                          
}


+(MyDocumentsFolder *) addFolder: (NSString *)name{
    NSNumber *folderId = [NSNumber numberWithInteger:[[MyDocumentsFolder getMaxFolderId] intValue] +100];    
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    MyDocumentsFolder *folder = (MyDocumentsFolder *)[NSEntityDescription insertNewObjectForEntityForName:@"MyDocumentsFolder" inManagedObjectContext: managedObjectContext];
    [folder setFolderId: folderId];
    [folder setName:name];
    return folder;
}

+(void)deleteFolder:(MyDocumentsFolder *)folder
{    
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    [managedObjectContext deleteObject:folder];
    [appDelegate saveContext];    
}


+(MyDocumentsFolder *)getFolderWithId:(NSNumber *)folderId
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyDocumentsFolder" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"folderId == %@", folderId];
    [request setPredicate:predicate];
    
    
    NSError *errors = nil;
    NSArray *array = [managedObjectContext executeFetchRequest:request error:&errors];
    [request release];
    if([array count] == 0)
        return nil;
    MyDocumentsFolder *result = [array objectAtIndex:0];
    return result;
}

+(NSArray *)getFolderWithName:(NSString *)name andFolderId:(NSNumber *)folderId
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyDocumentsFolder" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity]; 
    NSPredicate *predicate;
    if(folderId==nil){
    predicate = [NSPredicate predicateWithFormat:@"name == %@",name];
    }else {
    predicate = [NSPredicate predicateWithFormat:@"folderId == %@ && name == %@",folderId,name];
    }
    [request setPredicate:predicate];
    
    NSError *errors = nil;
    NSArray *array = [managedObjectContext executeFetchRequest:request error:&errors];
    [request release];
    if([array count] == 0)
        return nil;
    return array;
}

+(MyDocumentsFolder *)getDefaultFolder
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyDocumentsFolder" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"defaultFolder == %@", [NSNumber numberWithBool:YES]];
    [request setPredicate:predicate];
    
    
    NSError *errors = nil;
    NSArray *array = [managedObjectContext executeFetchRequest:request error:&errors];
    [request release];
    if([array count] == 0)
        return nil;
    MyDocumentsFolder *result = [array objectAtIndex:0];
    return result;
}

+(NSNumber *)getSize:(NSNumber *)folderId{
    return nil;
}



@end
